import Vue from 'vue'
import Router from 'vue-router'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
// import store from 'vuex';
// import store from '@/store/index.js';
import { messages } from '@/utils/msg-box.js';
import home from '@/pages/home/home.vue'
import login from '@/pages/loginPage/login.vue'
import register from '@/pages/loginPage/register.vue'
import about from '@/pages/about/about.vue'
import user from '@/pages/user/user.vue'

Vue.use(Router)

const routes =  [
	{
		path: '/',
		name: 'home',
		component: home
	},
    {
		path: '/home',
		name: 'home',
		component: home,
		children: [

		]
	},
	{
		path: '/blogDetails',
		name: 'blogDetails',
		component: ()=> import('@/pages/blogDetails/blogDetails.vue')
	},
	{
		path: '/login',
		name: 'login',
		component: login
	},
	{
		path: '/register',
		name: 'register',
		component: register
	},
	{
		path: '/about',
		name: 'about',
		component: about
	},
    {
		path: '/user',
		name: 'user',
		component: user,
		children: [
			{
				path: '/',
				name: '我的博文',
				component: ()=> import('@/pages/user/blogList/blogList.vue')
			},
			{
				path: '/setting',
				name: '用户设置',
				component: ()=> import('@/pages/user/setting/setting.vue')
			},
			{
				path: '/blogList',
				name: 'blogList',
				component: ()=> import('@/pages/user/blogList/blogList.vue')
			},
			{
				path: '/draftList',
				name: '草稿箱',
				component: ()=> import('@/pages/user/draftList/draftList.vue')
			},
		]
	},
	{
		path: '/blog',
		name: 'blog',
		component: ()=> import('@/pages/blog/blog.vue')
	},
	
    
    
]
var router = new Router({
	mode: "history",
    routes
})

const whiteList = ['/home','/login','/register','/about','/blogDetails']

const getToken = () => {
	let token = ''
	try {
		token = JSON.parse(localStorage.getItem('CLOUD_SHARE_TOKEN')).token;
	} catch (e) {
		//TODO handle the exception
	}
	return token
}
router.beforeEach(async(to, from, next) => {
    NProgress.start();
	const token = getToken()
	// const isLogin = await store.getters.getLogin
    if (token) {
		// 用户信息不存在
		// if (!store.getters.userInfo) {
		// 	// await store.dispatch('GetUser'); // 获取用户信息
		// 	// router.addRoutes(store.getters.addRoutes);
		// } else {
			
		// }
		next();
    } else {
		if (whiteList.indexOf(to.path) > -1) {
			// 在免登录白名单，直接进入
			next();
		} else {
			// window.location.href = store.getters.api.IPORTAL_LOCAL_API;
			messages('error', '登录失效，请重新登录');
			router.push({path:'/login'})
			NProgress.done();
		}
    }
});
router.afterEach(() => {
	NProgress.done()
})
export default router